package com.jblog.common.page;

import java.util.List;

import static com.jblog.Constants.Template.DEFAULT_PAGE_CSS_CLASS;

import com.jblog.common.page.common.Element;
import com.jblog.common.page.common.PageType;
import com.jblog.common.page.logic.ComplexLogic;
import com.jblog.common.page.logic.ConsecutiveLogic;
import com.jblog.common.page.logic.PageLogic;
import com.jblog.common.page.logic.SimpleLogic;

public class SimplePage implements Pageable {

    private final int defaultPageSize = 10;
    private List<Element> elements;

    protected String cssClass = DEFAULT_PAGE_CSS_CLASS;
    protected Long totalCount = 0L;
    protected Integer pageSize = 10;
    protected Integer pageNo = 1;
    protected Integer step = -1;
    
    private PageLogic logic;
    private PageType pageType;
    
    public SimplePage(Integer pageNo, Integer pageSize, Long totalCount) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNo(pageNo);
        setPageType(PageType.SIMPLE);//must at here
        setStep(logic.getStep());
    }
    public SimplePage(Integer pageNo, Integer pageSize, Long totalCount, PageType pageType) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNo(pageNo);
        setPageType(pageType);//must at here
        setStep(logic.getStep());
    }
    public SimplePage(Integer pageNo, Integer pageSize, Long totalCount, Integer step, PageType pageType) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNo(pageNo);
        setStep(step);
        setPageType(pageType);//must at here
    }
    
    /* -------------------------------------------------------- */

    public void setPageType(PageType pageType) {
        this.pageType = pageType;
        selector(pageType);
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }
    public void setTotalCount(Long totalCount) {
        this.totalCount = totalCount < 0L ? 0L : totalCount;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize < 1 ? defaultPageSize : pageSize;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo < 1 ? 1 : pageNo;
        this.pageNo = this.pageNo > getTotalPage() ? getTotalPage() : this.pageNo;
    }
    
    private void setStep(int step) {
        this.step = step;
    }
    
    private void selector(PageType pageType) {
        switch (pageType) {
        case COMPLEX:
            this.logic = new ComplexLogic(getTotalCount(), getTotalPage(), getPageSize(), getPageNo(), getStep());
            break;
        case CONSECUTIVE:
            this.logic = new ConsecutiveLogic(getTotalCount(), getTotalPage(), getPageSize(), getPageNo(), getStep());
            break;
        case SIMPLE:
        default:
            this.logic = new SimpleLogic(getTotalCount(), getTotalPage(), getPageSize(), getPageNo(), getStep());
        }
    }

    /* -------------------------------------------------------- */

    @Override
    public Long getTotalCount() {
        return totalCount;
    }

    @Override
    public String getCssClass() {
        return cssClass;
    }
    @Override
    public PageType getPageType() {
        return pageType;
    }
    @Override
    public Integer getTotalPage() {
        return Integer.valueOf(String.valueOf(totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1));
    }

    @Override
    public Integer getPageSize() {
        return pageSize;
    }

    @Override
    public Integer getPageNo() {
        return pageNo;
    }
    
    @Override
    public Integer getStep() {
        return step;
    }
    @Override
    public List<Element> getElements() {
        elements = logic.execute();
        return elements;
    }

}
